//
//  MYWebService.m
//  投融保
//
//  Created by 523 on 13-12-28.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "MYWebService.h"

#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
#import "Encryption.h"
#import "CRSA.h"
#import "NSData+NSData_HexAdditions.h"


@implementation MYWebService

NSDictionary *test;
@synthesize profileDataAsyn,huaChuDic,huaRuDic,touZiDic;
@synthesize profileDataSyn;

static MYWebService *sharedWebService;
-(id)initWithDelegate:(id<ServiceHelperDelegate>)theDelegate
{
	if (self=[super init]) {
		self.delegate=theDelegate;
	}
	return self;
}

//+(MYWebService *)sharedWebService
//{
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        sharedWebService = [[MYWebService alloc] init];
//    });
//    
//    return sharedWebService;
//}


- (void)getDataAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }

    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        profileDataAsyn = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getAsyData" object:[profileDataAsyn valueForKey:@"result"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getAsyData" object:[profileDataAsyn valueForKey:@"result"]];
    }];
    [request startAsynchronous];
}

//公用异步请求
- (void)publicGetDataAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        profileDataAsyn = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
        [self.delegate finishSuccessRequest:[profileDataAsyn valueForKey:@"result"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
       
    }];
    [request startAsynchronous];
}


- (NSDictionary *)getDataSynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }

    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:30];
    [request startSynchronous];
    if ([request responseData]!=nil) {
        NSLog(@"同步请求结果：%@",request.responseString);
        profileDataSyn = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        
    }
    
    return [profileDataSyn valueForKey:@"result"];
}

//异步队列
- (void)getDataAsynQueueWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey{
    ASINetworkQueue *queue = [[ASINetworkQueue alloc] init];

    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:30];
    [queue addOperation:request];
    queue.maxConcurrentOperationCount = 1;
    [queue go];

}
//项目列表
- (void)getTradeListAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    NSLog(@"securityKey-----%@",securityKey);
    NSLog(@"encriptdata=-------,%@",[cipher stringWithHexBytes1]);
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        profileDataAsyn = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getAsyData" object:[profileDataAsyn valueForKey:@"result"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"getAsyData" object:[profileDataAsyn valueForKey:@"result"]];
    }];
    [request startAsynchronous];
}
//异步获取投资记录
- (void)getTouZiAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        touZiDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"请求成功返回的参数：%@",touZiDic);
        [self.delegate finishTouZiRequest:[[touZiDic valueForKey:@"result"] valueForKey:@"infos"]];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
    }];
    [request startAsynchronous];
}
//得到划入记录
- (void)getHuaRuAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        huaRuDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
        
        [self.delegate finishHuaRuRequest:[[huaRuDic valueForKey:@"result"] objectForKey:@"infos"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");

    }];
    [request startAsynchronous];
}
//得到划出记录
- (void)getHuaChuAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        NSLog(@"+++++++++++++++请求成功结果+++++++++++++++");
        NSLog(@"请求成功返回的参数：%@",request.responseString);
        huaChuDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"+++++++++++++++请求成功结果end+++++++++++++");
        [self.delegate finishHuaChuRequest:[[huaChuDic valueForKey:@"result"] valueForKey:@"infos"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
    }];
    [request startAsynchronous];
}
//未完成项目异步
- (void)getUnfinishAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        
        self.proUnfinishDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        [self.delegate proUnfinishRequest:[self.proUnfinishDic valueForKey:@"result"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
    }];
    [request startAsynchronous];
}
//已完成项目异步
- (void)getProFinishAsynWithKey:(NSString *)key encryptedString:(NSString *)encryptedString appKey:(NSString *)appKey
{
    //    AES加密
    NSData *plain = [encryptedString dataUsingEncoding:NSUTF8StringEncoding];//将用户注册填写信息转换为data
    NSData *cipher = [plain AES256EncryptWithKey:key]; //通过key 对aes加密
    //rsa加密解密
    CRSA *_rsa = [CRSA shareInstance];
    NSString *securityKey = [_rsa encryptByRsa:key withKeyType:KeyTypePublic];
    
    
    __weak ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:URL];
    if (appKey!=nil) {
        [request setPostValue:appKey forKey:@"appKey"];
    }
    
    [request setPostValue:securityKey forKey:@"securityKey"];//rsa加密之后的字段 value为需要传给服务器的rsa加密之后的值
    [request setPostValue:[cipher stringWithHexBytes1] forKey:@"encriptData"];//value 为用户信息aes加密之后需要传给服务器的数据
    [request setTimeOutSeconds:60];
    [request setCompletionBlock:^{
        self.proFinishDic = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
        [self.delegate proFinishRequest:[self.proFinishDic valueForKey:@"result"]];
    }];
    [request setFailedBlock:^{
        NSLog(@"-----------------请求失败结果-----------------");
        NSLog(@"请求失败%@",request.responseString);//请求失败返回数据；
        NSLog(@"-----------------请求失败结果end--------------");
    }];
    [request startAsynchronous];
}

@end
